package com.mall.common.utils;

import com.mall.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author xyl
 * @date 2021-02-10 17:19
 */
public class Security {
    /**
     * 获取当前用户
     *
     * @return
     */
    public static Authentication getCurrentUserAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    public static Object getCurrentPrincipal() {
        return getCurrentUserAuthentication().getPrincipal();
    }

    public static Integer getCurrentUserId() {
        if(getCurrentPrincipal() instanceof User){
            return ((User) getCurrentPrincipal()).getUserId();
        }else{
            return -1;
        }
    }
}
